{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "under development"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(Z):\n",
    "    Z = np.array(Z)\n",
    "    return 1 / (1 + np.exp(-Z))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=np.array([[1,1],[1,0],[0,1],[0,0]])\n",
    "y=np.array([0,1,1,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "Z = np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "W1 = np.zeros((2,2))\n",
    "B1 = np.zeros((2,))\n",
    "\n",
    "W2 = np.zeros((2, 1))\n",
    "B2 = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def model(X, W1, B1, W2, B2):\n",
    "    Z1 = np.dot(X, W1) + B1\n",
    "    A1 = sigmoid(Z1)\n",
    "    print(A1)\n",
    "    Z2 = np.dot(A1, W2) + B2\n",
    "    print(Z2)\n",
    "    A2 = sigmoid(Z2)\n",
    "    \n",
    "    return A2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.5 0.5]\n",
      " [0.5 0.5]\n",
      " [0.5 0.5]\n",
      " [0.5 0.5]]\n",
      "[[0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0.5],\n",
       "       [0.5],\n",
       "       [0.5],\n",
       "       [0.5]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model(X, W1, B1, W2, B2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = []\n",
    "A.append(X)\n",
    "A.append(np.array([1,1]))\n",
    "A.append(0)\n",
    "A = np.array(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = {}\n",
    "A[1] = X\n",
    "A[2] = np.array([0.1,0.11])\n",
    "A[3] = 0\n",
    "\n",
    "W = {}\n",
    "W[1] = np.array([[0.1, 0.11], [0.12, 0.13]])\n",
    "W[2] = np.array([0.1, 0.11]).reshape((2,1))\n",
    "\n",
    "B = {}\n",
    "B[1] = np.array([0.11, 0.12])\n",
    "B[2] = np.array(0.11)\n",
    "\n",
    "Z = {}\n",
    "Z[2] = np.array([0.1,0.11])\n",
    "Z[3] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "def forward_propagate(A, Z, W, B):\n",
    "    Z[2] = np.dot(A[1], W[1]) + B[1]\n",
    "    A[2] = sigmoid(Z[2])\n",
    "    Z[3] = np.dot(A[2], W[2]) + B[2]\n",
    "    A[3] = sigmoid(Z[3])\n",
    "    return A, Z, W, B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "A, Z, W, B = forward_propagate(A, Z, W, B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({1: array([[1, 1],\n",
       "         [1, 0],\n",
       "         [0, 1],\n",
       "         [0, 0]]),\n",
       "  2: array([[0.58175938, 0.58904043],\n",
       "         [0.55230791, 0.55724785],\n",
       "         [0.55724785, 0.5621765 ],\n",
       "         [0.5274723 , 0.52996405]]),\n",
       "  3: array([[0.55798059],\n",
       "         [0.55639108],\n",
       "         [0.5566468 ],\n",
       "         [0.55503691]])},\n",
       " {2: array([[0.33, 0.36],\n",
       "         [0.21, 0.23],\n",
       "         [0.23, 0.25],\n",
       "         [0.11, 0.12]]),\n",
       "  3: array([[0.23297039],\n",
       "         [0.22652805],\n",
       "         [0.2275642 ],\n",
       "         [0.22104328]])},\n",
       " {1: array([[0.1 , 0.11],\n",
       "         [0.12, 0.13]]),\n",
       "  2: array([[0.1 ],\n",
       "         [0.11]])},\n",
       " {1: array([0.11, 0.12]), 2: array(0.11)})"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A, Z, W, B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "D={}\n",
    "D[3] = A[3].T - y\n",
    "D[3] = D[3].reshape((4,1))\n",
    "D[2] = np.multiply(np.dot(D[3], W[2].T), np.multiply(A[2], 1-A[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3: array([[ 0.55798059],\n",
       "        [-0.44360892],\n",
       "        [-0.4433532 ],\n",
       "        [ 0.55503691]]),\n",
       " 2: array([[ 0.01357653,  0.01485785],\n",
       "        [-0.01096885, -0.01203932],\n",
       "        [-0.01093853, -0.01200368],\n",
       "        [ 0.01383403,  0.0152087 ]])}"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.05579806, -0.04436089, -0.04433532,  0.05550369],\n",
       "       [ 0.06137787, -0.04879698, -0.04876885,  0.06105406]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(W[2], D[3].T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.2433154 , 0.2420718 ],\n",
       "       [0.24726388, 0.24672268],\n",
       "       [0.24672268, 0.24613408],\n",
       "       [0.24924527, 0.24910216]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(A[2], 1-A[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "W = []\n",
    "W.append(np.array([[0.1, 0.11], [0.12, 0.13]]))\n",
    "W.append(np.array([[0.111, 0.112]]))\n",
    "W = np.array(W)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = []\n",
    "B.append(np.array([[0.1, 0.11]]))\n",
    "B.append(np.array([[0.111, 0.112]]))\n",
    "B = np.array(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "Z = []\n",
    "Z.append(np.array([1, 1, 1, 1]))\n",
    "Z.append(np.array([1, 1,]))\n",
    "Z.append(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 1, 1, 0],\n",
       "       [1, 1, 0, 0],\n",
       "       [1, 0, 1, 0],\n",
       "       [0, 0, 0, 0]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(X,X.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cross_entropy(y, y_hat):\n",
    "    n_samples = y.shape[0]\n",
    "    return sum(-y*np.log(y_hat)-(1-y)*np.log(1-y_hat))/n_samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cost_function(X, y, W1, B1, W2, B2):\n",
    "    y_hat = model(X,  W1, B1, W2, B2)\n",
    "    return cross_entropy(y, y_hat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def transfer_derivative(A):\n",
    "    return A * (1-A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-36-0f02a54d350e>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-36-0f02a54d350e>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def forward_propagate(A, W1, B1, W2, B2):\u001b[0m\n\u001b[1;37m                                             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
